﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Linq;
using CodeGenerator.Utils;
using System;

namespace CodeGenerator
{
    class ConfigUtil
    {
        private readonly string _fileName;
        private JArray _data = null;
        public static readonly string ConnFile = "conns.json";
        public static readonly string TemplateFile = "templates.json";
        public static readonly string OutputPathFile = "outputPath.json";
        public static readonly string AppConfigFile = "appConfig.json";
        public ConfigUtil(string fileName)
        {
            var dir = "configs/";
            this._fileName = CommonUtil.GenFullPath(dir + fileName);
            Directory.CreateDirectory(CommonUtil.GenFullPath(dir));
            _data = GetConfigs();
        }
        public JArray GetConfigs()
        {
            if (_data != null) return _data;
            if (!File.Exists(_fileName))
            {
                File.WriteAllText(_fileName, "[]");
            }
            var content = File.ReadAllText(_fileName);
            return new JArray(JArray.Parse(content).OrderBy(x => x["id"]));
        }
        public JObject GetConfig(object id)
        {
            var data = GetConfigs();
            var list = data.Where(x => x["id"].ToString() == id.ToString()).ToList();
            if (list.Count == 0) return null;
            return (JObject)list[0];
        }
        public int AddConfig(object obj)
        {
            var res = GetConfigs();
            JObject jsonObj = ToJsonObj(obj);
            var id = res.Count + 1;
            jsonObj.Add("id", id);
            res.Add(jsonObj);
            SaveConfigs(res);
            return id;
        }
        public JObject ToJsonObj(object obj)
        {
            string jsonStr = JsonConvert.SerializeObject(obj);
            return JsonConvert.DeserializeObject<JObject>(jsonStr);
        }
        public void SaveConfigs(JArray data)
        {

            using (var sw = new StringWriter())
            using (var writer = new JsonTextWriter(sw) { QuoteName = false, Formatting = Formatting.Indented })
            {
                var serializer = new JsonSerializer();
                serializer.Serialize(writer, data);
                File.WriteAllText(_fileName, sw.ToString());
            }
            this._data = null;
            this._data = GetConfigs();
            Console.WriteLine(11);
        }
        public void UpdateConfig(object obj)
        {
            var res = GetConfigs();
            var json = ToJsonObj(obj);
            var item = res.FirstOrDefault(x => x["id"].ToString() == json["id"].ToString());
            res.Remove(item);
            res.Add(json);
            SaveConfigs(res);
        }
        public void RemoveConfig(object obj)
        {
            var json = ToJsonObj(obj);
            RemoveConfigById((string)json["id"]);
        }
        public void RemoveConfigById(string id)
        {
            var res = GetConfigs();
            var item = res.FirstOrDefault(x => (string)x["id"] == id);
            res.Remove(item);
            SaveConfigs(res);
        }
        public void RemoveConfigById(int id)
        {
            var res = GetConfigs();
            var item = res.FirstOrDefault(x => (int)x["id"] == id);
            res.Remove(item);
            SaveConfigs(res);
        }
        public void RemoveAll()
        {
            SaveConfigs(new JArray());
        }
    }
}
